package com.xiaolin.ssosystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaolin.ssosystem.model.entity.User;
import com.xiaolin.ssosystem.model.dto.UserDto;
import com.xiaolin.ssosystem.service.AuthService;
import com.xiaolin.ssosystem.service.UserService;
import com.xiaolin.ssosystem.utils.ContextUtil;
import com.xiaolin.ssosystem.utils.OptResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaolin
 * @description 描述
 * @since 2024/10/8
 */
@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private final String authToken = "auth-token";

    @Override
    public OptResult login(UserDto userDto, HttpServletResponse response) {
        // 校验用户名密码是否正确
        User user = userService.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserAccount, userDto.getUserAccount())
                .eq(User::getPwd, userDto.getPwd()));
        if (Objects.isNull(user)) {
            return new OptResult(400, null);
        }
        String token = UUID.randomUUID().toString();
        // 保存用户信息到Redis
        redisTemplate.opsForValue().set(token, user, 30, TimeUnit.MINUTES);
        // 给前端派发一个cookie
        Cookie cookie = new Cookie(authToken, token);
        // 设置域名
//        cookie.setDomain("localhost");
        cookie.setDomain("sso.system.com");
        // 设置路径
        cookie.setPath("/");
        // 设置超时时间
        long seconds = TimeUnit.MINUTES.toSeconds(60 * 24);
        cookie.setMaxAge((int) seconds);
        response.addCookie(cookie);
        return new OptResult(200, null);
    }

    @Override
    public OptResult info(HttpServletRequest request) {
        String token = ContextUtil.getToken(request, authToken);
        if (StringUtils.isEmpty(token)) {
            return new OptResult(400, null);
        }
        User user = (User) redisTemplate.opsForValue().get(token);
        if (Objects.isNull(user)) {
            return new OptResult(400, null);
        }
        Map<String, Object> data = new HashMap<>();
        data.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        data.put("introduction", "I am a super administrator");
        data.put("name", user.getName());
        data.put("roles", Collections.singletonList("admin"));
        return new OptResult(200, data);
    }

    @Override
    public OptResult logout(HttpServletRequest request) {
        String token = ContextUtil.getToken(request, authToken);
        if (StringUtils.isEmpty(token)) {
            return new OptResult(400, null);
        }
        redisTemplate.delete(token);
        return new OptResult(200, null);
    }

}
